//Francisco "Kiko" Olvera
//handles input from keyboard and mouse
//input.h

#include "input.h"

void Input::init(HWND hwnd, HINSTANCE hInst)
{
	DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDIObject, NULL); 
	m_pDIObject->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboard, NULL);
	m_pDIObject->CreateDevice(GUID_SysMouse, &m_pDIMouse, NULL);

	m_pDIKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	m_pDIMouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 

	m_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
	m_pDIMouse->SetDataFormat(&c_dfDIMouse2);
	wndHandle = hwnd;
	mDown = false;
}

void Input::Update()
{
	m_pDIKeyboard->Acquire();
    m_pDIKeyboard->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);
    m_pDIMouse->Acquire();
	m_pDIMouse->GetDeviceState(sizeof(DIMOUSESTATE2), &mouseState);
}

bool Input::KeyPressed(int num)
{
	if( (buffer[num] & 0x80) && (!key[num]) )
	{
		key[num] = true;
		return true;
	}
	if(!(buffer[num] & 0x80) )
	{
		key[num] = false;
	}
	return false;
}

bool Input::KeyDown(int num)
{
	if(buffer[num] & 0x80)
		return true;
	return false;
}

POINT Input::getMousePosition()
{
	POINT pos;
	GetCursorPos(&pos);
	ScreenToClient(wndHandle, &pos);
	return pos;
}

bool Input::leftClicked()
{
	if(mouseState.rgbButtons[0] & 0x80)
	{
		if(!mDown)
		{
			mDown = true;
			return true;
		}
		else
			return false;
	}
	else
		mDown = false;
	return false;
}

bool Input::rightClicked()
{
	if(mouseState.rgbButtons[1] & 0x80)
	{
		if(!mDown)
		{
			mDown = true;
			return true;
		}
		else
			return false;
	}
	else
		mDown = false;
	return false;
}